package com.xayy.health.mgt.service.impl;

import cn.kawins.base.utils.BeanUtil;
import com.xayy.health.core.exception.HealthException;
import com.xayy.health.mgt.mapper.TelemetryDataMapper;
import com.xayy.health.mgt.model.TaskInfo;
import com.xayy.health.mgt.model.TaskParamInfo;
import com.xayy.health.mgt.model.TelemetryData;
import com.xayy.health.mgt.model.dto.DataFeatureDto;
import com.xayy.health.mgt.model.dto.TelemetryDto;
import com.xayy.health.mgt.service.SatelliteNodeService;
import com.xayy.health.mgt.service.SelectTelemetryDataSourceService;
import com.xayy.health.mgt.service.TaskInfoService;
import com.xayy.health.mgt.service.TaskParamInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 可选数据源（原始数据，数据处理后数据）
 * @author shichenlei
 */
@Service
@Slf4j
public class SelectTelemetryDataSourceServiceImpl implements SelectTelemetryDataSourceService {

    @Autowired
    private SatelliteNodeService satelliteNodeService;

    @Autowired
    private TaskParamInfoService taskParamInfoService;

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private TelemetryDataMapper telemetryDataMapper;

    /**
     * 获取原始的遥测数据
     * @param dataFeatureDto 查询遥测参数
     * @return 原始的遥测数据结果集
     */
    @Override
    public List<TelemetryData> getOriginalYcList(DataFeatureDto dataFeatureDto) {
        List<TelemetryData> ycList = satelliteNodeService.getYcList(dataFeatureDto);
        for(TelemetryData data:ycList){
          if(StringUtils.isEmpty(data.getValue())){
              throw new HealthException("参数 "+ data.getParamCode()+ " 原始遥测数据中存在空值......");
          }
        }
        return ycList;
    }


    /**
     * 运行任务时选择处理后数据
     * @param telemetryDto 查询处理后数据参数
     * @return 遥测数据集合
     */
    public Map<String, List<TelemetryData>> selectTelemetryDataByRunTask(TelemetryDto telemetryDto) {
        Map<String, List<TelemetryData>> result = new HashMap<>(1);
        if (BeanUtil.isEmpty(telemetryDto) || telemetryDto.getStartTime() == null || telemetryDto.getEndTime() == null) {
            return result;
        }
        String[] paramCodes = telemetryDto.getParamCodes();
        for (String paramCode : paramCodes) {
            List<TelemetryData> telemetryDataList = new ArrayList<>();
            TaskParamInfo taskParamInfo = taskParamInfoService.selectOne(TaskParamInfo.builder().satelliteCode(telemetryDto.getSatelliteCode()).paramCode(paramCode).status(1).build());
            if (BeanUtil.isNotEmpty(taskParamInfo)) {
                TaskInfo taskInfo = taskInfoService.selectById(taskParamInfo.getTaskId());
                if (null != taskInfo) {
                    telemetryDataList = getTelemetry(telemetryDto, taskInfo, paramCode);
                }
            }
            result.put(paramCode, telemetryDataList);
        }
        return result;
    }

    /**
     * 事后诊断和趋势预测所用入参数据获取
     *
     * @param telemetryDto 入参
     * @param taskInfo     任务时间
     * @param paramCode    参数
     * @return 结果
     */
    private List<TelemetryData> getTelemetry(TelemetryDto telemetryDto, TaskInfo taskInfo, String paramCode) {
        Date startTimeTelemetry = telemetryDto.getStartTime();
        Date endTimeTelemetry = telemetryDto.getEndTime();
        DataFeatureDto dataFeatureDto = DataFeatureDto.builder().startTime(startTimeTelemetry).endTime(endTimeTelemetry).satelliteCode(telemetryDto.getSatelliteCode()).paramCode(paramCode).parentTaskId(taskInfo.getId()).build();
        return telemetryDataMapper.getPhList(dataFeatureDto);
    }

}
